import localForage from "localforage";
import ajax from '@/utils/ajaxiView';

const apiConfigKey = 'apiConfig';

export default {
  find() {
    return localForage.getItem(apiConfigKey).then(function(value) {
      if(value === null) {
        return ajax.get('/admin/Config/index').then(function(res) {
          if(res !== false) {
            localForage.setItem(apiConfigKey, res.config).then(function(value) {
              return value;
            }).catch(function(error) {
              console.log(error);
            });

            return res.config;
          }

          return null;
        });
      }

      return value;
    }).catch(function(error) {
      console.log(error);
    });
  },
  remove() {
    localForage.removeItem(apiConfigKey).then(function() {
      console.log(apiConfigKey + ' is remove');
    }).catch(function(err) {
      console.log(err);
    });
  }
};
